//
//  BViewController.swift
//  mySwift
//
//  Created by X-MEN on 2021/12/6.
//

import UIKit

class BlockWithAsyncAwaitViewController: UIViewController {
    //B包
    var didSelItemBlock:((_ model : tempModel,_ str : String) -> ())?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.prefersLargeTitles = false;
        self.navigationController?.navigationItem.largeTitleDisplayMode = .automatic;
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.navigationItem.title = "BBlock"
        
        //        if let didSelItemBlock = self.didSelItemBlock {
        //            didSelItemBlock(tempModel(),"hahaha")
        //        }
        //        // Do any additional setup after loading the view.
        //        let age = getAgeWithName1(name: "张珊") {[weak self] (dict) in
        //            print(dict ?? "");
        //        }
        //        let age2 =  getAgeWithName2(name: "", handle: {
        //            dict in
        //        })
        //        print(age);
        //
        //        let success = loadSuccess { str in
        //            print(str);
        //        }
        //        print(success);
        
//                someSyncMethod()
//                processFromScratch()
                
        
        Task {
            let aa = await loadData1()
            if (aa.count > 0) {
                print("结果回来了")
            }
        }
        print("kank")
    }
    
    func processFromScratch() {
        Task {
            async let loadData1 = loadData1()
            async let loadData2 = loadData2()
            async let loadData3 = loadData3()
            await [loadData3, loadData1, loadData2]
            print("请求完成processFromScratch")
        }
        
    }
    
    func someSyncMethod() {
        Task {
            await withThrowingTaskGroup(of: Void.self) { group in
                group.addTask {
                    await self.loadData1()
                }
                group.addTask {
                    await self.loadData2()
                }
                group.addTask {
                    await self.loadData3()
                }
            }
            //           let str = await self.loadData1()
            print("请求完成someSyncMethod")
        }
    }
    
    func loadData1() async -> String {
        return await withCheckedContinuation { continuation in
            DispatchQueue.global().async {
                print(Thread.current)
                for i in 0..<1000000 {
                }
                print("loadData1")
                continuation.resume(returning: "loadData1")
            }
        }
    }
    
    func loadData2() async -> String {
        return await withCheckedContinuation { continuation in
            DispatchQueue.global().async {
                print(Thread.current)
                for i in 0..<100000 {
                }
                print("loadData2")
                continuation.resume(returning: "loadData2")
            }
        }
    }
    
    func loadData3() async -> String {
        return await withCheckedContinuation { continuation in
            DispatchQueue.global().async {
                print(Thread.current)
                for i in 0..<10 {
                }
                print("loadData3")
                continuation.resume(returning: "loadData3")
            }
        }
    }
    
}

extension BlockWithAsyncAwaitViewController {
    typealias completionHandle = (Dictionary<String,Any>?) -> ()
    
    func getAgeWithName1(name:String,handle : @escaping (_ dict : [String : Any]?) -> ()) -> String {
        handle(["name" : "老张"])
        return "18"
    }
    
    
    
    func getAgeWithName2(name:String,handle :@escaping completionHandle) -> String {
        handle(["name" : "老张"])
        return "18"
    }
    
    func loadSuccess(success : (String) -> ()) -> Bool {
        success("成功")
        return true
    }
    
    
    
    
}
